package com.jic.market.impl;

import com.google.common.collect.Lists;
import com.jic.common.base.vo.RestResult;
import com.jic.market.common.enums.DeleteFlagEnum;
import com.jic.market.common.exception.MarketErrorCodeEnum;
import com.jic.market.common.exception.MarketException;
import com.jic.market.entity.PromotionEntity;
import com.jic.market.entity.PromotionRuleEntity;
import com.jic.market.enums.FrontPromTypeConditionTypeEnum;
import com.jic.market.mapper.PromotionEntityMapper;
import com.jic.market.mapper.PromotionRuleEntityMapper;
import com.jic.market.request.PromotionMerchantRequest;
import com.jic.market.request.PromotionRuleRequest;
import com.jic.market.response.PromotionRuleResponse;
import com.jic.market.service.PromotionRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author guohuayong
 * @date: 2020年02月20日
 */
@Service
@Slf4j
public class PromotionRuleServiceImpl implements PromotionRuleService {
    @Autowired
    PromotionRuleEntityMapper curMapper;
    @Autowired
    PromotionEntityMapper promotionEntityMapper;


    @Override
    public List<PromotionRuleResponse> findAll(PromotionRuleRequest.PromotionRuleListRequest listRequest) {
        PromotionRuleEntity entity = new PromotionRuleEntity();
        BeanUtils.copyProperties(listRequest, entity);
        entity.setCreatorName(null);
        entity.setCreator(null);
        entity.setUpdator(null);
        entity.setUpdatorName(null);
        entity.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
        List<PromotionRuleEntity> list = curMapper.findAll(entity);
        List<PromotionRuleResponse> t= Optional.ofNullable(list).orElse(Lists.newArrayList())
               .stream().map(item->{
            PromotionEntity promotionEntity = promotionEntityMapper.queryById(item.getPromotionId());
            FrontPromTypeConditionTypeEnum frontPromTypeConditionTypeEnum = FrontPromTypeConditionTypeEnum.lookByFrontType(promotionEntity.getFrontPromType());
            PromotionRuleResponse ret = new PromotionRuleResponse();
            ret.setConditionValue(item.getConditionValue() != null ? new BigDecimal(item.getConditionValue()).divide(frontPromTypeConditionTypeEnum.getConditionMultiply()) : null);
            ret.setContentValue(item.getContentValue() != null ? item.getContentValue().divide(frontPromTypeConditionTypeEnum.getContentValueMultiply()) : null);
            BeanUtils.copyProperties(item, ret);
            return ret;
        }).collect(Collectors.toList());
        return t;
    }

    @Override
    public Integer saveBatch(PromotionRuleRequest request) {
        if (null == request.getRuleListRequestList() || request.getRuleListRequestList().isEmpty()) {
            throw new MarketException(MarketErrorCodeEnum.PARAM_ERROR);
        }
        for (int i = 0; i < request.getRuleListRequestList().size(); i++) {
            PromotionRuleRequest.PromotionRuleListRequest temp = request.getRuleListRequestList().get(i);
            PromotionRuleEntity entity = new PromotionRuleEntity();
            BeanUtils.copyProperties(temp, entity);
            Integer ret = curMapper.insert(entity);
            if (ret < 1) {
                throw new MarketException(MarketErrorCodeEnum.SQL_ERROR);
            }
        }
        return request.getRuleListRequestList().size();
    }

    @Override
    public List<PromotionRuleResponse> save(PromotionRuleRequest.PromotionRuleListRequest request) {
        PromotionRuleEntity entity = new PromotionRuleEntity();
        PromotionRuleEntity param = new PromotionRuleEntity();
        param.setPromotionId(request.getPromotionId());
        param.setDeleteFlag(0);
        BeanUtils.copyProperties(request, entity);
        Integer c = curMapper.insert(entity);
        List<PromotionRuleEntity> all = curMapper.findAll(param);
        List<PromotionRuleResponse> collect = all.stream().map(x -> {
            PromotionRuleResponse ret = new PromotionRuleResponse();
            BeanUtils.copyProperties(x, ret);
            return ret;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<PromotionRuleResponse> delete(PromotionRuleRequest.PromotionRuleListRequest request) {
        PromotionRuleEntity entity = new PromotionRuleEntity();
        PromotionRuleEntity param = new PromotionRuleEntity();
        param.setPromotionId(request.getPromotionId());
        param.setDeleteFlag(0);
        entity.setId(request.getId());
        entity.setDeleteFlag(1);
        curMapper.update(entity);
        List<PromotionRuleEntity> all = curMapper.findAll(param);
        List<PromotionRuleResponse> collect = all.stream().map(x -> {
            PromotionRuleResponse ret = new PromotionRuleResponse();
            BeanUtils.copyProperties(x, ret);
            return ret;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public Integer update(PromotionRuleRequest.PromotionRuleListRequest request) {
        PromotionRuleEntity entity = new PromotionRuleEntity();
        entity.setId(request.getId());
        entity.setConditionValue(request.getConditionValue() != null ? request.getConditionValue().multiply(new BigDecimal(100)).longValue() : null);
        curMapper.update(entity);
        return entity.getId().intValue();
    }
}
